//
//  KDProvinceController.m
//  PublicSentimentMonitorSystem
//
//  Created by 魏强 on 15/8/10.
//  Copyright (c) 2015年 魏强. All rights reserved.
//

#import "KDProvinceController.h"
#import "KDCityController.h"
#import "KDReginType.h"
#import "KDReginDataTool.h"
#import "KDReportController.h"
@interface KDProvinceController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) NSArray *infoArr;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation KDProvinceController
#pragma mark - 懒加载区
- (NSArray *)infoArr{
    if (!_infoArr) {
        NSArray *arr = (self.fromWeibo)?[KDReginDataTool weiboReginTypeArrWithParentId:0]:[KDReginDataTool reginTypeArrWithParentId:0];
        KDReginType *model = [[KDReginType alloc]init];
        model.name = @"全国";
        model.ID = 0;
        model.parentId =0;
        NSMutableArray *mutableArr = [NSMutableArray arrayWithArray:arr];
        [mutableArr insertObject:model atIndex:0];
        _infoArr = [NSArray arrayWithArray:mutableArr];
    }
    return _infoArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"全国";
    self.view.backgroundColor = BackGroundColor;
    [self setUpTableView];
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
}
- (void)setUpTableView{
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(10  , 10, self.view.width -20, self.view.height - 64 -10) style:UITableViewStylePlain];
    tableView.tableFooterView = [UIView new];
    tableView.separatorColor = BackGroundColor;
    self.tableView = tableView;
    //    tableView.editing = YES;
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}
#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.infoArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *cellId =@"provinceCell";
    
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    KDReginType *model = self.infoArr[indexPath.row];
    cell.textLabel.text = model.name;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    KDReginType *model = self.infoArr[indexPath.row];
    if (indexPath.row == 0) {
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"ProvinceAddressToReportControllerNoty" object:nil userInfo:@{@"address":@"全国",@"regionModel":model}];
        UIViewController *VC ;
        for (UIViewController *vc in self.navigationController.viewControllers) {
            if ([vc isKindOfClass:[KDBaseRegionController class]] ) {
                VC = vc;break;
            }
        }
        [self.navigationController popToViewController:VC
                                              animated:YES];
        
        return;
    }
    KDCityController *cityCV = [[KDCityController alloc]init];
    cityCV.fromWeibo = self.fromWeibo;
    cityCV.ReginType = model;
    cityCV.address = model.name;
    
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:cityCV animated:YES];
    self.hidesBottomBarWhenPushed = NO;
    
}


@end
